package com.xc.controller;

import com.xc.pojo.Emp;
import com.xc.pojo.FindByPage;
import com.xc.pojo.PageBean;
import com.xc.pojo.Result;
import com.xc.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 员工管理Controller
 */
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    //@GetMapping
    //public Result selectByPage(Integer pageSize,Integer pageCount){
    //    PageBean pageBean = empService.selectByPage(pageSize, pageCount);
    //    return Result.success(pageBean);
    //}

    @GetMapping
    public Result selectAll(FindByPage findByPage) {
        PageBean pageBean = empService.selectAllAndCondation(findByPage);
        return Result.success(pageBean);
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        Emp emp = empService.selectById(id);
        if (emp == null)
            return Result.error("查询失败，客官再来呀");
        else
            return Result.success(emp);
    }

    @PostMapping
    public Result insert(@RequestBody Emp emp) {
        return Result.success(empService.insert(emp));
    }

    @PutMapping
    public Result update(@RequestBody Emp emp) {
        int effectRows = empService.update(emp);
        if (effectRows == 0)
            return Result.error("更新失败，客官再来呀");
        else
            return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable Integer[] ids) {
        int effectRows = empService.delete(ids);
        if (effectRows == 0)
            return Result.error("删除失败咯，客官再来呀");
        else
            return Result.success();
    }
}
